<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>客户管理</el-breadcrumb-item>
                <el-breadcrumb-item>会员优惠券</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="couponFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="couponFormData.couponName" placeholder="请输入优惠券名称"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-select v-model="couponFormData.couponType" clearable placeholder="请选择优惠券类型">
                        <el-option
                                v-for="item in couponTypeData"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item >
                    <el-select v-model="couponFormData.platform" clearable placeholder="请选择使用平台">
                        <el-option
                                v-for="item in platformData"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item >
                    <el-select v-model="couponFormData.useType" clearable placeholder="请选择使用类型">
                        <el-option
                                v-for="item in useTypeData"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getAllCoupon()">查询</el-button>
                    <el-button type="primary" @click="insertCoupon()">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="couponId"
                            label="优惠券ID">
                    </el-table-column>
                    <el-table-column
                            prop="couponName"
                            label="优惠券名称">
                    </el-table-column>
                    <el-table-column
                            prop="couponType"
                            label="优惠券类型">
                        <template slot-scope="scope">
                            <span v-if="scope.row.couponType==0">全场赠券</span>
                            <span v-if="scope.row.couponType==1">会员赠券</span>
                            <span v-if="scope.row.couponType==2">注册赠券</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="platform"
                            label="使用平台">
                        <template slot-scope="scope">
                            <span v-if="scope.row.platform==0">全部平台</span>
                            <span v-if="scope.row.platform==1">移动平台</span>
                            <span v-if="scope.row.platform==2">PC平台</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="couponAmount"
                            label="优惠金额">
                    </el-table-column>
                    <el-table-column
                            prop="limitedNumber"
                            label="限领张数">
                    </el-table-column>
                    <el-table-column
                            prop="useThreshold"
                            label="使用门槛">
                    </el-table-column>
                    <el-table-column
                            prop="startTime"
                            label="开始时间">
                    </el-table-column>
                    <el-table-column
                            prop="endTime"
                            label="结束时间">
                    </el-table-column>
                    <el-table-column
                            prop="useType"
                            label="使用类型">
                        <template slot-scope="scope">
                            <span v-if="scope.row.useType==0">全场通用</span>
                            <span v-if="scope.row.useType==1">指定类型</span>
                            <span v-if="scope.row.useType==2">指定车辆</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="receiveCount"
                            label="领取数量">
                    </el-table-column>
                    <el-table-column
                            prop="useCount"
                            label="已使用数量">
                    </el-table-column>
                    <el-table-column
                            prop="code"
                            label="优惠券码">
                    </el-table-column>
                    <el-table-column
                            prop="note"
                            label="备注">
                    </el-table-column>
                    <el-table-column label="操作" >
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle  @click="updateCoupon(scope.row)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteCouponById(scope.row.couponId)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pageUtil.currentPage"
                        :page-sizes="[5,10,15,20]"
                        :page-size="pageUtil.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>

            <el-dialog
                    title="优惠券信息"
                    :visible.sync="dialogVisible"
                    width="30%"
                    :before-close="handleClose">
                <span>
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

                        <el-input v-model="ruleForm.couponId" readonly="readonly" type="hidden"></el-input>

                      <el-form-item label="优惠券名称" prop="couponName">
                        <el-input v-model="ruleForm.couponName"></el-input>
                      </el-form-item>
                        <el-form-item label="优惠券类型" prop="couponType">
                        <el-select v-model="ruleForm.couponType" clearable placeholder="请选择优惠券类型">
                            <el-option
                                    v-for="item in couponTypeData"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                      </el-form-item>
                        <el-form-item label="使用平台" prop="platform">
                        <el-select v-model="ruleForm.platform" clearable placeholder="请选择使用平台">
                            <el-option
                                    v-for="item in platformData"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                      </el-form-item>
                        <el-form-item label="优惠券金额" prop="couponAmount">
                        <el-input v-model="ruleForm.couponAmount"></el-input>
                      </el-form-item>
                        <el-form-item label="使用门槛" prop="useThreshold">
                        <el-input v-model="ruleForm.useThreshold"></el-input>
                      </el-form-item>
                        <el-form-item label="开始时间" prop="startTime">
                        <el-date-picker
                                v-model="ruleForm.startTime"
                                type="date"
                                placeholder="选择开始时间"
                                format="yyyy 年 MM 月 dd 日"
                                value-format="yyyy-MM-dd">
                        </el-date-picker>
                      </el-form-item>
                        <el-form-item label="结束时间" prop="endTime">
                        <el-date-picker
                                v-model="ruleForm.endTime"
                                type="date"
                                placeholder="选择结束时间"
                                format="yyyy 年 MM 月 dd 日"
                                value-format="yyyy-MM-dd">
                        </el-date-picker>
                      </el-form-item>
                        <el-form-item label="使用类型" prop="useType">
                        <el-select v-model="ruleForm.useType" clearable placeholder="请选择使用类型">
                            <el-option
                                    v-for="item in useTypeData"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                      </el-form-item>
                        <el-form-item label="备注" prop="note">
                        <el-input type="textarea" v-model="ruleForm.note"></el-input>
                      </el-form-item>
                      <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                      </el-form-item>
                    </el-form>
                </span>
            </el-dialog>

        </el-card>
    </div>
</template>

<script>
    import qs from 'qs';

    export default {
        name: "ShowCoupon",

        data(){
            return {

                // 表单
                ruleForm: {
                    couponId:'',
                    couponName:'',
                    couponType:'',
                    platform:'',
                    couponAmount:'',
                    useThreshold:'',
                    startTime:'',
                    endTime:'',
                    useType:'',
                    note:''
                },

                // 验证规则
                rules: {
                    couponName: [
                        { required: true, message: '请输入优惠券名称', trigger: 'blur' },
                    ],
                    couponAmount: [
                        { required: true, message: '请输入消费券金额', trigger: 'blur' }
                    ],
                    startTime: [
                        { required: true, message: '请选择开始时间', trigger: 'blur' },
                    ],
                    endTime: [
                        { required: true, message: '请选择结束时间', trigger: 'blur' },
                    ],
                    couponType: [
                        { required: true, message: '请选择优惠券类型', trigger: 'blur' },
                    ],
                    useThreshold:[
                        { required: true, message: '请选择使用门槛', trigger: 'blur' },
                    ],
                    useType:[
                        { required: true, message: '请选择使用类型', trigger: 'blur' },
                    ],
                    platform:[
                        { required: true, message: '请选择使用平台', trigger: 'blur' },
                    ],

                },

                // 搜索下拉菜单数据
                couponTypeData:[
                    {
                        value:0,
                        label:'全场赠券'
                    },
                    {
                        value:1,
                        label:'会员赠券'
                    },
                    {
                        value:2,
                        label:'注册赠券'
                    }
                ],
                platformData:[
                    {
                        value:0,
                        label:'全部平台'
                    },
                    {
                        value:1,
                        label:'移动平台'
                    },
                    {
                        value:2,
                        label:'PC平台'
                    }
                ],
                useTypeData:[
                    {
                        value:0,
                        label:'全场通用'
                    },
                    {
                        value:1,
                        label:'指定类型'
                    },
                    {
                        value:2,
                        label:'指定车辆'
                    }
                ],

                //条件搜索对象
                couponFormData:{
                    couponName:'',
                    couponType:'',
                    platform:'',
                    useType:''
                },

                //表格数据
                tableData:[],

                // 分页
                pageUtil:{
                    //每页显示的条数
                    pageSize: 5,
                    //当前的显示的页码
                    currentPage:1,
                },
                //总条数
                total:0,
                // 弹出层
                dialogVisible:false
            }
        },

        methods:{

            // 关闭弹出层
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {});
            },

            // 修改优惠券
            updateCoupon(row) {
                this.dialogVisible = true;
                this.ruleForm = row;
            },

            // 新增优惠券
            insertCoupon() {
                this.dialogVisible = true;
            },

            // 提交表格
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios({
                            method:'put',
                            url:'clms-client/coupon/updateCoupon',
                            data:qs.stringify(this.ruleForm)
                        }).then(r=>{
                            if (r.data.code == 200) {
                                this.$message.success(r.data.message);
                                // 刷新表格
                                this.getAllCoupon();
                                this.dialogVisible = false;
                            }
                        })
                    } else {
                        this.$message.error(r.data.message);
                        return false;
                    }
                });
            },

            // 重置表格
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },

            // 根据id删除优惠券
            deleteCouponById(couponId) {
                this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios({
                        method:'delete',
                        url:'clms-client/coupon/deleteCouponById',
                        params:{
                            couponId: couponId
                        }
                    }).then(r=>{
                        if (r.data.code == 200) {
                            this.$message.success(r.data.message);
                            // 刷新表格
                            this.getAllCoupon();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            // 获取优惠券信息
            getAllCoupon() {
                this.$axios({
                    method:'post',
                    url:'clms-client/coupon/getAllCoupon',
                    params:{
                        currentPage: this.pageUtil.currentPage,
                        pageSize: this.pageUtil.pageSize
                    },
                    data:qs.stringify(this.couponFormData)
                }).then(r=>{
                    if (r.data.code == 200) {
                        this.tableData = r.data.object.records;
                        this.total = r.data.object.total;
                    }
                })
            },
            // 改变每页显示的条数时触发该方法
            handleSizeChange(val){
                this.pageUtil.pageSize = val;
                this.getAllCoupon();
            },
            //切换页码时触发该方法
            handleCurrentChange(val){
                this.pageUtil.currentPage = val;
                this.getAllCoupon();
            }
        },

        // 钩子函数
        created() {
            // 根据条件查询优惠券信息
            this.getAllCoupon();
        }

    }
</script>

<style scoped>

</style>